package com.ayf.payment.game.api.service.impl;

import com.ayf.payment.game.api.entity.IdGenerator;
import com.ayf.payment.game.api.mapper.IdGeneratorMapper;
import com.ayf.payment.game.api.service.IdGeneratorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;

@Service
public class IdGeneratorServiceImpl implements IdGeneratorService{

    //自增3位，用于补全操作
    private static final String STR_FORMAT = "000";

    @Resource
    private IdGeneratorMapper idGeneratorMapper;

    @Override
    public String getIdGenerator() {
        IdGenerator idGenerator = new IdGenerator();
        idGeneratorMapper.insert(idGenerator);
        //位数不够，前面补0
        DecimalFormat df = new DecimalFormat(STR_FORMAT);
        return df.format(idGenerator.getId());
    }

    @Override
    public void resetIdGenerator() {
        idGeneratorMapper.truncatIdGenerator();
        idGeneratorMapper.resetIdGenerator();
    }
}
